/**
 * @filename:AgentConfigServiceImpl 2025-05-14
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.agent.service.impl;

import com.starhub.engine.generator.genid.IdGenerator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.starhub.application.interfaces.agent.AgentConfigService;
import com.starhub.common.bean.agent.AgentConfig;
import com.starhub.common.bean.agent.PromptVectorConfig;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.agent.dao.AgentConfigDao;
import com.starhub.engine.agent.dao.PromptVectorConfigDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**   
 * @Description:(智能体设置服务实现)
 *
 * @version: V1.0
 * @author: luosw
 * 
 */
@Service
public class AgentConfigServiceImpl implements AgentConfigService {

    @Autowired
    private AgentConfigDao agentDao;

    @Autowired
    private PromptVectorConfigDao promptVectorConfigMapper;

    /**
    * 新增
    * @param user
    * @param agent
    * @return
    */
    public Long add(LoginUser user, AgentConfig agent){

        Long id = IdGenerator.generateId();

        //数据封装，默认值添加
        agent.setId(id);
        agent.setCreateTime(new Date());
        agent.setUpdateTime(new Date());
        agent.setDeleteType(0);

        agent.setFileUpload(Boolean.parseBoolean(agent.getFileUpload()) ? "1" : "0");
        agent.setIsStreaming(Boolean.parseBoolean(agent.getIsStreaming()) ? "1" : "0");
        agent.setShowWelcomeGuide(Boolean.parseBoolean(agent.getShowWelcomeGuide()) ? "1" : "0");

        agentDao.insertSelective(agent);
        return id;
    }

    /**
    * 更新
    * @param user
    * @param agent
    * @return
    */
    public AgentConfig update(LoginUser user, AgentConfig agent){
        agent.setUpdateTime(new Date());
        //不允许更新的属性设置为null

        agent.setFileUpload(Boolean.parseBoolean(agent.getFileUpload()) ? "1" : "0");
        agent.setIsStreaming(Boolean.parseBoolean(agent.getIsStreaming()) ? "1" : "0");
        agent.setShowWelcomeGuide(Boolean.parseBoolean(agent.getShowWelcomeGuide()) ? "1" : "0");

        agentDao.updateByPrimaryKeySelective(agent);
        return agent;
    }

    /**
    * 基本信息获取
    * @param user
    * @param id
    * @return
    */
    public AgentConfig getInfo(LoginUser user, Long id){

        AgentConfig agent = agentDao.selectByPrimaryKey(id);
        return agent;
    }

    @Override
    public List<PromptVectorConfig> getVectorConfigById(Long agentId) {
        List<PromptVectorConfig> list = promptVectorConfigMapper.selectList(new LambdaQueryWrapper<PromptVectorConfig>()
                .eq(PromptVectorConfig::getAgentId, agentId)
                .eq(PromptVectorConfig::getDeleteType, 0));
        return list;
    }

    /**
    * (批量)删除，同时支持单条或者批量,此方法为逻辑删除
    * @param user
    * @param ids
    */
    public void deleteByType(LoginUser user, String ids,int deleteType){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
            //删除状态，目前只有1的逻辑
        agentDao.deleteAgentConfigByType(idList, deleteType);
    }

    /**
    * 根据id删除，此方法为真实删除
    * @param user
    * @param ids
    */
    public void deleteRecord(LoginUser user, String ids){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        agentDao.deleteAgentConfigRecord(idList);
    }

    /**
    * 列表查询
    * @param user
    * @param params
    */
    public List<AgentConfig> getList(LoginUser user,  Map<String , Object>  params){

        List<AgentConfig> agentList =  new ArrayList<AgentConfig>();
            //TODO
        int pageSize = 10;
        int current = 1;
        AgentConfig record = new AgentConfig();
        record.setDeleteType(0);
        agentList = agentDao.selectByPrimaryKeySelective(record);
        return agentList;
    }

    @Override
    public AgentConfig getById(Long agentId) {
        AgentConfig agent = agentDao.selectByPrimaryKey(agentId);
        return agent;
    }

    @Override
    public AgentConfig getByIdentifier(String identifier) {
        AgentConfig agent = agentDao.selectByIdentifier(identifier);
        return agent;
    }
}